﻿#include "StoningStatue.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../MapObject.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    StoningStatue::StoningStatue(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool StoningStatue::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 2 || y > 2)
        {
            return false;
        }

        return (x <= 1 && y <= 1) || (x == y || x % 2 == y % 2);
    }

    void StoningStatue::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        if (_areaTime == std::numeric_limits<long long>::max())
        {
            _areaTime = getEnvir()->getTime() + 10000;
        }

        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));

        if (getEnvir()->getTime() < _areaTime)
        {
            S::ObjectAttack *tempVar = new S::ObjectAttack();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            tempVar->Type = 0;
            Broadcast(tempVar);
            LineAttack(AttackRange);

            ActionTime = getEnvir()->getTime() + 300;
            AttackTime = getEnvir()->getTime() + AttackSpeed;

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            return;
        }

        _areaTime = getEnvir()->getTime() + 5000 + getEnvir()->Random->Next(10) * 1000;

        ActionTime = getEnvir()->getTime() + 500;
        AttackTime = getEnvir()->getTime() + (AttackSpeed * 2);

        S::ObjectAttack *tempVar2 = new S::ObjectAttack();
        tempVar2->ObjectID = ObjectID;
        tempVar2->Direction = getDirection();
        tempVar2->Location = getCurrentLocation();
        tempVar2->Type = 1;
        Broadcast(tempVar2);

        auto damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]);
        if (damage == 0)
        {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            return;
        }

        DelayedAction tempVar3(DelayedType::Damage, getEnvir()->getTime() + 1600, {getTarget(), 0, DefenceType::MACAgility, true});
        ActionList.push_back(&tempVar3);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
    }

    void StoningStatue::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);
        bool area = data.size() >= 4 && std::any_cast<bool>(data[3]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        if (!area)
        {
            target->Attacked(this, damage, defence);
        }
        else
        {
            std::vector<MapObject*> targets = FindAllTargets(2, getCurrentLocation(), false);
            if (targets.empty())
            {
                return;
            }

            for (int i = 0; i < targets.size(); i++)
            {
                targets[i]->Attacked(this, Stats[Stat::MaxMC], defence);

                PoisonTarget(targets[i], 2, damage, PoisonType::Dazed, 1000);
            }
        }
    }
}
